package junit;

import org.junit.*;

import static org.junit.Assert.* ;

import fast.PeerManager;

public class PeerManagerTest {
	
	private PeerManager peer;

	@Test
	public void check_add_peer(){
		peer.addPeer("10.1.0.5", 5000);
		
		System.out.println("Test: check_add_peer 1 of 2");
		System.out.println("******************");
		assertTrue(peer.contains("10.1.0.5"));
		System.out.println("test passed.\n\n");
		
		System.out.println("Test: check_add_peer 2 of 2");
		System.out.println("******************");
		assertFalse(peer.contains("10.1.0.10"));
		System.out.println("test passed.\n\n");
	}
	
	@Test
	public void check_remove_peer(){
		System.out.println("Test: check_remove_peer 1 of 1");
		System.out.println("******************");
		peer.removePeer("10.1.0.1");
		assertFalse(peer.contains("10.1.0.1"));
		System.out.println("test passed.\n\n");
	}
	
	@Test
	public void check_get_port(){
		for(int i =1; i<5; i++){
			System.out.println("Test: check_get_port " + i + " of 6");
			System.out.println("******************");
			int s = peer.getPort("10.1.0."+String.valueOf(i));
			assertTrue(s == (i*1000));
			System.out.println("test passed.\n\n");
		}
		
		System.out.println("Test: check_get_port 6 of 6");
		System.out.println("******************");
		assertTrue(peer.getPort("10.1.0.10") == 10000);
		System.out.println("test passed.\n\n");
		
	}
	
	@Test
	public void check_get_all(){
		
		String[][] s = peer.getAllPeers();
		
		System.out.println("Test: check_get_all 1 of 1");
		System.out.println("******************");
		assertTrue(s.length == 4);
		System.out.println("test passed.\n\n");

	}
	
	@Test
	public void check_get_count(){
		System.out.println("Test: check_get_count 1 of 2");
		System.out.println("******************");
		assertTrue(peer.getCount() == 4);
		System.out.println("test passed.\n\n");
		
		peer.addPeer("10.1.0.6", 6000);
		System.out.println("Test: check_get_count 1 of 2");
		System.out.println("******************");
		assertTrue(peer.getCount() == 5);
		System.out.println("test passed.\n\n");
	}
	
	
	@Before public void load_state(){
		peer = new PeerManager();
		
		peer.addPeer("10.1.0.1",1000);
		peer.addPeer("10.1.0.2",2000);
		peer.addPeer("10.1.0.3",3000);
		peer.addPeer("10.1.0.4",4000);
	}

}
